class Fountain extends egret.Sprite {
    private count: number = 100
    private gravity: number = 0.5
    private wind:number = 0.2
    private balls: Array<Ball>

    public constructor() {
        super()
        this.once(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this)
    }

    private onAddToStage(): void {
        this.stage.scaleMode = egret.StageScaleMode.NO_SCALE
        this.balls = new Array<Ball>()
        for (let i: number = 0; i < this.count; i++) {
            let ball: Ball = new Ball(2, Math.random() * 0xFFFFFF)
            ball.x = this.stage.stageWidth / 2
            ball.y = this.stage.stageHeight
            ball.vx = Math.random() * 2 - 1
            ball.vy = Math.random() * -10 - 10
            this.addChild(ball)
            this.balls.push(ball)
        }
        this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this)
    }

    private onEnterFrame(event: egret.Event): void {
        for (let i: number = this.balls.length - 1; i >= 0; i--) {
            let ball: Ball = this.balls[i]
            ball.vy += this.gravity
            ball.vx += this.wind
            ball.x += ball.vx
            ball.y += ball.vy
            if (ball.x - ball.radius > this.stage.stageWidth ||
                ball.x + ball.radius < 0 ||
                ball.y - ball.radius > this.stage.stageHeight ||
                ball.y + ball.radius < 0) {
                ball.x = this.stage.stageWidth / 2
                ball.y = this.stage.stageHeight
                ball.vx = Math.random() * 2 - 1
                ball.vy = Math.random() * -10 - 10
            }
        }
    }
}